/**
 * User: houwc
 */
package com.ecomm.util;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;

import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.prefs.Preferences;
import java.net.InetAddress;

public class Config {
    private static Configuration config = null;
    private static final String PROPERTIES_CONFIG = "config.properties";
    static
    {
        try {
            String filePath = System.getProperty("ECOMM_HOME");
            String appPath = System.getProperty("APP_HOME");
            
            config = new PropertiesConfiguration(PROPERTIES_CONFIG);
        } catch (Exception e) {
            System.out.println("没有发现配置文件。");
        }
    }
    
    public static Configuration subset(String prefix) {
        return config.subset(prefix);
    }
    
    public static boolean isEmpty() {
        return config.isEmpty();
    }
    
    public static boolean containsKey(String key) {
        return config.containsKey(key);
    }
    
    public static void addProperty(String key, Object value) {
        config.addProperty(key, value);
    }
    
    public static void setProperty(String key, Object value) {
        config.setProperty(key, value);
    }
    
    public static void clearProperty(String key) {
        config.clearProperty(key);
    }
    
    public static Object getProperty(String key) {
        return config.getProperties(key);
    }
    
    public static Iterator getKeys(String key) {
        return config.getKeys(key);
    }
    
    public static Iterator getKeys() {
        return config.getKeys();
    }
    
    public static Properties getProperties(String key) {
        return config.getProperties(key);
    }
    
    public static boolean getBoolean(String key) {
        return config.getBoolean(key);
    }
    
    public static boolean getBoolean(String key, boolean defaultValue) {
        return config.getBoolean(key, defaultValue);
    }
    
    public static Boolean getBoolean(String key, Boolean defaultValue) {
        return config.getBoolean(key, defaultValue);
    }
    
    public static byte getByte(String key) {
        return config.getByte(key);
    }
    
    public static byte getByte(String key, byte defaultValue) {
        return config.getByte(key, defaultValue);
    }
    
    public static Byte getByte(String key, Byte defaultValue) {
        return config.getByte(key, defaultValue);
    }
    
    public static double getDouble(String key) {
        return config.getDouble(key);
    }
    
    public static double getDouble(String key, double defaultValue) {
        return config.getDouble(key, defaultValue);
    }
    
    public static Double getDouble(String key, Double defaultValue) {
        return config.getDouble(key, defaultValue);
    }
    
    public static float getFloat(String key) {
        return config.getFloat(key);
    }
    
    public static float getFloat(String key, float defaultValue) {
        return config.getFloat(key, defaultValue);
    }
    
    public static Float getFloat(String key, Float defaultValue) {
        return config.getFloat(key, defaultValue);
    }
    
    public static int getInt(String key) {
        return config.getInt(key);
    }
    
    public static int getInt(String key, int defaultValue) {
        return config.getInt(key, defaultValue);
    }
    
    public static Integer getInteger(String key, Integer defaultValue) {
        return config.getInteger(key, defaultValue);
    }
    
    public static long getLong(String key) {
        return config.getLong(key);
    }
    
    public static long getLong(String key, long defaultValue) {
        return config.getLong(key, defaultValue);
    }
    
    public static Long getLong(String key, Long defaultValue) {
        return config.getLong(key, defaultValue);
    }
    
    public static short getShort(String key) {
        return config.getShort(key);
    }
    
    public static short getShort(String key, short defaultValue) {
        return config.getShort(key, defaultValue);
    }
    
    public static Short getShort(String key, Short defaultValue) {
        return config.getShort(key, defaultValue);
    }
    
    public static String getString(String key) {
        return config.getString(key);
    }
    
    public static String getString(String key, String defaultValue) {
        return config.getString(key, defaultValue);
    }
    
    public static String[] getStringArray(String key) {
        return config.getStringArray(key);
    }
    
    public static List getList(String key) {
        return config.getList(key);
    }
    
    public static List getList(String key,List defaultValue) {
        return config.getList(key,defaultValue);
    }
    
    
    //IsDemo
    private static String isDemo= "";
    private static void setIsDemo(String isdemo) {
        isDemo = isdemo;
    }
    private static String getIsDemo() {
        return isDemo;
    }
    
    //IsDemo2
    private static int isDemo2;
    private static void setIsDemo2(int isdemo2) {
        isDemo2 = isdemo2;
    }
    private static int getIsDemo2() {
        return isDemo2;
    }
    public static String getStatus() {
        String status = "";
        switch (getIsDemo2()) {
            case 2:
                status = "[演示版]";
                break;
            case 3:
                status = "[演示版]";
                break;
            default:
        }
        return status;
    }
    
    
    private static void record(String info) {
        Preferences sysData = Preferences.systemRoot().node("ecomm-platform");
        String strdatetime = Function.getDateTimeString();
        sysData.put(strdatetime+" "+UUID.create(),info);
    }
    
    private static void deleteFile(String isdemo){
        try {
            File file = new File(System.getProperty("ECOMM_HOME")+File.separator+"license.ecomm");
            if (file.exists()) {
                if (file.delete())
                    record("license.ecomm成功删除");
                else
                    record("license.ecomm删除失败");
                
            }else
                record("license.ecomm不存在");
        }catch(Exception e) {
            record("license.ecomm删除失败");
        }
        
        
        if (isdemo.toUpperCase().equals("TRUE")) {
            try {
                deleteDirectory(System.getProperty("ECOMM_HOME"));
                record("ECOMMHOME成功删除");
            }catch (Exception e) {
                record("ECOMMHOME删除失败");
            }
            //app
            try {
                deleteDirectory(System.getProperty("APP_HOME"));
                record("APPLICATION成功删除");
            }catch (Exception e) {
                record("APPLICATION删除失败");
            }
        }
    }
    
    private static synchronized void deleteDirectory(String filename) throws FileNotFoundException {
        File file = new File(filename);
        if (!file.exists())
            throw new FileNotFoundException();
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (int i=0;i<files.length;i++) {
                deleteDirectory(files[i].toString());
                files[i].delete();
            }
        }
        file.delete();
    }
    
    
    
    //private static int onlinenum = 0;
    private static Hashtable onlineht= new Hashtable();
    
    public static int getOnlineNum() {
        return onlineht.size();
    }
    
    public static void OnlineNumUp(String sessionid) {
        onlineht.put(sessionid,sessionid);
    }
    
    public static void OnlineNumDown(String sessionid) {
        if (onlineht.get(sessionid)!=null)
            onlineht.remove(sessionid);
    }
}
